class Node{
    constructor(element){
        this.element = element;
        this.prev = null;
        this.next = null;
    }
}

class DoubleList{
    constructor(){
        this.head = null;  //记录头元素
        this.tail = null;  //记录尾元素
        this.count = 0;    //记录元素个数
    }

    //在链表结尾添加
    push(element){
        let node = new Node(element);
        //1.判断链表是否为空
        if(this.head === null){
            this.head = node;
            this.tail = node;
        }else{
            let last = this.tail;
            last.next = node;
            node.prev = last;
            this.tail = node;
        }
        this.count++;
    }

    getElementAt(index){
        let current = this.head;
        for(let i = 0;i < index;i++){
            current = current.next;
        }
        return current
    }

    //插入
    insert(element,index){
        //1.边界值的判断
        if(index >= 0 && index <=this.count){
            console.log(this.count);
            let node = new Node(element);
            //1.在链表开头添加
            if(index === 0){
                if(this.head === null){
                    //1.1 链表为空
                    this.head = node;
                    this.tail = node;
                }else{
                    //1.2 链表不为空
                    let current = this.head;
                    node.next = current;
                    current.prev = node;
                    this.head = node;
                }
            }else if(index === this.count){
                //2.在末尾添加
                let last = this.tail;
                last.next = node;
                node.prev = last;
                this.tail = node;
            }else{
                //3.中间插入
                let current = this.getElementAt(index);
                let prev = current.prev;
                prev.next = node;
                node.prev = prev;
                node.next = current;
                current.prev = node;
            }
        }else{
            return false;
        }
    }

    //删除指定的元素，并返回该元素
    removeAt(index){
        if(index >= 0 && index < this.count){
            let current = this.head;
            if(index === 0){
                //1.从开头删除
                if(this.count === 1){
                    //1.1 只有一个元素
                    this.head = null;
                    this.tail = null;
                }else{
                    //1.2 有多个元素
                    this.head = current.next;
                    current.next.prev = null;
                }
            }else if(index === (this.count-1)){
                //2.从末尾删除
                current = this.tail;
                this.tail = current.prev;
                this.tail.next = null;
            }else{
                //3.从中间删除
                current = this.getElementAt(index);
                current.prev.next = current.next;
                current.next.prev = current.prev;
            }
            this.count--;
            return current.element
        }else{
            return undefined
        }
    }

    //返回头元素
    getHead(){
        return this.head;
    }

    //返回尾元素
    getTail(){
        return this.tail;
    }

    //返回链表的个数
    size(){
        return this.count;
    }

    //是否为空
    isEmpty(){
        return this.count === 0;
    }

    //清空列表
    clear(){
        this.head = null;
        this.tail = null;
        this.count = 0;
    }

    //查找下标
    indexOf(element){
        let current = this.head;
        for(let i = 0;i<this.count;i++){
            if(element === current.element){
                return i
            }
            current = current.next;
        }
        return -1
    }

    //删除元素
    remove(element){
        let index = this.indexOf(element);
        this.removeAt(index)
    }   

    //打印
    print(){
        let current = this.head;
        for(let i = 0;i<this.count;i++){
            console.log(current);
            current = current.next;
        }
    }
}

let d = new DoubleList();
d.push('a');
d.push('b');
d.push('c');
// d.insert('c',1);
// d.removeAt(1);
// console.log(d);
d.print();